package com.hangim.pce.service.impl;

import com.hangim.pce.enums.TimeUnit;
import com.hangim.pce.mapper.SysPrescriptionMapper;
import com.hangim.pce.model.SysPrescription;
import com.hangim.pce.model.vo.PatientVo;
import com.hangim.pce.model.vo.request.SyncRecipeBody;
import com.hangim.pce.service.SysPrescriptionService;
import com.hangim.pce.common.service.impl.BaseEntityServiceImpl;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 基准处方表 服务实现类
 * </p>
 *
 * @author hongjiliang
 * @since 2018-10-19
 */
@Service
public class SysPrescriptionServiceImpl extends BaseEntityServiceImpl<SysPrescriptionMapper, SysPrescription> implements SysPrescriptionService {

    @Override
    public SysPrescription savePrescription(SyncRecipeBody syncRecipeBody, PatientVo patientVo) {
        SysPrescription prescription = new SysPrescription();

        if (patientVo != null && patientVo.getPeoples() != null && patientVo.getPeoples().size() >= 1) {
            StringBuffer temp = new StringBuffer();
            for (String string : patientVo.getPeoples()) {
                temp.append(string);
                temp.append(",");
            }
            prescription.setCrowd(temp.substring(0, temp.length() - 2));
        }
        prescription.setDiagnosis(patientVo.getDiagnoseDetail());
        prescription.setSex(Integer.parseInt(patientVo.getUserSex()));
        prescription.setUserAge(syncRecipeBody.getUserAge());
        prescription.setUserAgeUnit(TimeUnit.getTimeUnitByDesc(syncRecipeBody.getUserAgeUnit()).getNum());
        prescription.setCreatedDate(getTimestamp());
        prescription.setDoctorId(Long.parseLong(syncRecipeBody.getDoctorId()));
        prescription.setDoctor(syncRecipeBody.getDoctorName());
        prescription.setActive(true);
        save(prescription);
        return prescription;
    }
}
